package com.ruoyi.zsyh.task;


import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.zsyh.domain.ZsyhFactoryPay;
import com.ruoyi.zsyh.mapper.ZsyhFactoryPayMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

/**
 * 厂家服务费结转
 * */

@Component("factoryPayTask")
public class FactoryPayTask {

    @Autowired
    private ZsyhFactoryPayMapper zsyhFactoryPayMapper;

    //每月第一天，服务费自动结转
    public void payTaskNoParams()
    {
        ZsyhFactoryPay zsyhFactoryPay = new ZsyhFactoryPay();
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH )+1;
        if(month == 1){
            zsyhFactoryPay.setPeriodYear(year-1);
            zsyhFactoryPay.setPeriodMonth(12);
        }else{
            zsyhFactoryPay.setPeriodYear(year);
            zsyhFactoryPay.setPeriodMonth(month-1);
        }
        List<ZsyhFactoryPay> zsyhFactoryPayList = zsyhFactoryPayMapper.selectZsyhFactoryPayList(zsyhFactoryPay);
        if(zsyhFactoryPayList.size()>0){
            for(ZsyhFactoryPay temp: zsyhFactoryPayList){
                double payEnd = temp.getPayEnd().doubleValue();
//                if(temp.getPayEnd().compareTo(BigDecimal.ZERO) == 1){//如果期末未结算服务费大于0，则自动结转到下个月
                if(payEnd != 0){
                    //如果期末未结算服务费 不等于0，则自动结转到下个月
                    ZsyhFactoryPay newZsyhFactoryPay = new ZsyhFactoryPay();
                    newZsyhFactoryPay.setId(IdWorker.getId());
                    newZsyhFactoryPay.setPeriodYear(year);
                    newZsyhFactoryPay.setPeriodMonth(month);
                    newZsyhFactoryPay.setPayBegin(temp.getPayEnd());
                    newZsyhFactoryPay.setPayOn(new BigDecimal(0));
                    newZsyhFactoryPay.setPayOff(new BigDecimal(0));
                    newZsyhFactoryPay.setPayEnd(temp.getPayEnd());
                    newZsyhFactoryPay.setFactoryId(temp.getFactoryId());
                    newZsyhFactoryPay.setCreateTime(DateUtils.getNowDate());
                    zsyhFactoryPayMapper.insertZsyhFactoryPay(newZsyhFactoryPay);
                }
            }
        }
    }

}
